package com.kun.rental.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kun.rental.entity.CarrentalInfo;
import com.kun.rental.entity.Violation;
import com.kun.rental.service.ICarrentalInfoService;
import com.kun.rental.service.IViolationService;
import com.kun.rental.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 * 违章管理
 * @author ikun
 * @since 2024-07-04
 */
@RestController
@RequestMapping("/rental/violation")
public class ViolationController {

    @Autowired
    private IViolationService violationService;


    @Autowired
    private ICarrentalInfoService carrentalInfoService;

    /**
     * 分页违章查询
     * @param start
     * @param size
     * @param violation
     * @return
     */
    @PostMapping("/search/{start}/{size}")
    public Result search(@PathVariable int start,
                         @PathVariable int size,
                         @RequestBody Violation violation) {
        Page<Violation> page = new Page<>(start, size);
        return  Result.success(violationService.searchByPage(page,violation));
    }


    /**
     * 新增
     * 因为根据车牌号码查出对应的车辆信息，所以导入carrentalIndo
     * @param violation
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Violation violation){
        CarrentalInfo carrentalInfo =  carrentalInfoService.selectByCarrentalNum(violation.getNum());
        violation.setCarrentalId(carrentalInfo.getId());
        return violationService.save(violation)?Result.success():Result.fail();
    }


    /**
     * 修改
     * @param violation
     * @return
     */
    @PutMapping("/update")
    public Result update(@RequestBody Violation violation){
        CarrentalInfo carrentalInfo =  carrentalInfoService.selectByCarrentalNum(violation.getNum());
        violation.setCarrentalId(carrentalInfo.getId());
        return violationService.updateById(violation)?Result.success():Result.fail();
    }


    /**
     * 删除
     * @param ids
     * @return
     */
    @DeleteMapping("/delete/{ids}")
    public Result delete(@PathVariable String ids){
        return violationService.delete(ids)?Result.success():Result.fail();
    }


}
